package com.cloud.controller;

import com.cloud.model.T_base;
import com.cloud.model.T_result;
import com.cloud.service.BaseService;
import com.cloud.service.ResultService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

import io.swagger.annotations.ApiOperation;

/**
 * @Author wangjian (jian.wang@bertelsmann.com.cn)
 * @Date 2017/2/8 16:17
 */
@RestController
@RequestMapping("/v1/base")
public class BaseController {

    @Autowired
    BaseService baseService;

    @Autowired
    ResultService resultService;


    @ApiOperation(value="基础数据组装",notes="1.对2个月份的数据进行处理，次月的数据放入备份表. 2.先处理首月数据到结果表，再执行第二个接口方法处理次月数据到结果表并且对比")
    @RequestMapping(value="/updateBase",method= RequestMethod.POST)
    public void updateBase(){
        //查询基础数据 组装基础数据
        List<T_base> baseList=baseService.findAll();
        String rename="";
        for(T_base base:baseList){
            String username = base.getUsername();
            String uname = base.getUname();
            String unum=base.getUnum();
            if("".equals(uname)&&"".equals(unum)){
                rename=username;
            }
            if(!"".equals(uname)){
                base.setUsername(rename);
                baseService.updateBase(base);
            }
        }
        // 删除不需要的行
        T_base base = new T_base();
        base.setUname("");
        baseService.deleteBase(base);

        // 组装数据
        T_result tr = new T_result();
        List<T_result> trList =  resultService.findAll(tr);
        List<T_base> bList = baseService.findTotal();
        String username="";
        String fmonth="";

        if(trList.size()==0){
            int i=0;
            for(T_base b:bList){
                i++;
                if("".equals(username)){
                    username = b.getUsername();
                }
                int unum = Integer.parseInt(b.getUnum());
                if(username.equals(b.getUsername())&&unum>0){
                    fmonth=fmonth+b.getUname()+"/";
                }
                if(!username.equals(b.getUsername())){
                    T_result r = new T_result();
                    r.setUsername(username);
                    r.setFmonth(fmonth.substring(0, fmonth.length() - 1));
                    resultService.insert(r);
                    fmonth="";
                    username=b.getUsername();
                    fmonth=fmonth+b.getUname()+"/";
                }
                if(i==bList.size()){
                    T_result r = new T_result();
                    r.setUsername(username);
                    r.setFmonth(fmonth.substring(0,fmonth.length()-1));
                    resultService.insert(r);
                }
            }
        }else{
            int flag=0;
            for(T_base b:bList){
                flag++;
                if("".equals(username)){
                    username = b.getUsername();
                }
                int unum = Integer.parseInt(b.getUnum());
                if(username.equals(b.getUsername())&&unum>0){
                    fmonth=fmonth+b.getUname()+"/";
                }
                if(!username.equals(b.getUsername())){
                    T_result r = new T_result();
                    r.setUsername(username);
                    List<T_result> rList =  resultService.findAll(r);
                    if(rList.size()>0){
                        T_result re = new T_result();
                        re.setId(rList.get(0).getId());
                        re.setSmonth(fmonth.substring(0, fmonth.length() - 1));
                        resultService.updateResult(re);
                    }else{
                        T_result r1 = new T_result();
                        r1.setUsername(username);
                        r1.setSmonth(fmonth.substring(0,fmonth.length()-1));
                        resultService.insert(r1);
                    }
                    fmonth="";
                    username=b.getUsername();
                    fmonth=fmonth+b.getUname()+"/";
                }
                if(flag==bList.size()){
                    T_result r = new T_result();
                    r.setUsername(username);
                    List<T_result> rList =  resultService.findAll(r);
                    if(rList.size()>0){
                        T_result re = new T_result();
                        re.setId(rList.get(0).getId());
                        re.setSmonth(fmonth.substring(0, fmonth.length() - 1));
                        resultService.updateResult(re);
                    }else{
                        T_result r1 = new T_result();
                        r1.setUsername(username);
                        r1.setSmonth(fmonth.substring(0,fmonth.length()-1));
                        resultService.insert(r1);
                    }
                }
            }

            T_result result = new T_result();
            List<T_result> tList = resultService.findAll(result);
            for(T_result t:tList){
                if(t.getFmonth()==null&&t.getSmonth()!=null){
                    t.setMore(t.getSmonth());
                    t.setMorenum(String.valueOf(t.getSmonth().split("/").length));
                    resultService.updateResult(t);
                    continue;
                }else if(t.getFmonth()!=null&&t.getSmonth()==null){
                    t.setLess(t.getFmonth());
                    t.setLessnum(String.valueOf(t.getFmonth().split("/").length));
                    resultService.updateResult(t);
                    continue;
                }
                String b[] = t.getFmonth().split("/");
                String c[] = t.getSmonth().split("/");
                String d="";
                String e="";
                if(t.getFmonth().equals(t.getSmonth())){
                    continue;
                }
                for(int i=0;i<b.length;i++ ){
                    if(!Arrays.asList(c).contains(b[i])){
                        d=d+b[i]+"/";
                    }
                }

                for(int j=0;j<c.length;j++ ){
                    if(!Arrays.asList(b).contains(c[j])){
                        e=e+c[j]+"/";
                    }
                }
                if(!"".equals(d)||!"".equals(e)){

                    if(!"".equals(e)){
                        t.setMore(e.substring(0, e.length() - 1));
                        t.setMorenum(String.valueOf(e.substring(0, e.length() - 1).split("/").length));
                    }

                    if(!"".equals(d)){
                        t.setLess(d.substring(0, d.length() - 1));
                        t.setLessnum(String.valueOf(d.substring(0, d.length() - 1).split("/").length));
                    }
                    resultService.updateResult(t);
                }

            }
        }
    }

    @ApiOperation(value="基础数据组装2",notes="1.基础数据处理 2.价格统计 3.")
    @RequestMapping(value="/updateBase2",method= RequestMethod.POST)
    public void updateBas2e(){
        //查询基础数据 组装基础数据
        List<T_base> baseList=baseService.findAll();
        String rename="";
        for(T_base base:baseList){
            String username = base.getUsername();
            String uname = base.getUname();
            String unum=base.getUnum();
            if("".equals(uname)&&"".equals(unum)){
                rename=username;
            }
            if(!"".equals(uname)){
                base.setUsername(rename);
                baseService.updateBase(base);
            }
        }
        // 删除不需要的行
        T_base base = new T_base();
        base.setUname("");
        baseService.deleteBase(base);

        // 组装数据
        List<T_base> bList = baseService.findTotal();
        String username="";
        String fmonth="";
        String price="0";
        int i=0;
        for(T_base b:bList){
            if("".equals(username)){
                username = b.getUsername();
            }
            int unum = Integer.parseInt(b.getUnum());
            if(username.equals(b.getUsername())){
                fmonth=fmonth+b.getUname()+String.valueOf(unum)+"/";
                if(!"".equals(b.getPrice())){
                    price = String.valueOf(Double.valueOf(price)+Double.valueOf(b.getPrice()));
                }
            }else{
                T_result r = new T_result();
                r.setUsername(username);
                r.setFmonth(fmonth.substring(0, fmonth.length() - 1));
                r.setFprice(price);
                resultService.insert(r);
                fmonth="";
                username=b.getUsername();
                fmonth=fmonth+b.getUname()+String.valueOf(unum)+"/";
                price=b.getPrice();
            }
            i++;
            if(i==bList.size()){
                T_result r = new T_result();
                r.setUsername(username);
                r.setFmonth(fmonth.substring(0,fmonth.length()-1));
                r.setFprice(price);
                resultService.insert(r);
            }
        }
    }
}
